package evacuator;

import java.io.IOException;

import path.DistanceList;
import building.Cell;
import building.InfoAgent;
import building.InfoGame;
import jade.core.*;
import jade.domain.FIPAAgentManagement.FailureException;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.lang.acl.*;
import jade.proto.AchieveREResponder;

public class ReceiveFinishLoad {
	private InfoGame game;

	public void addBehaviour(Agent agent) {
		MessageTemplate mt1 = MessageTemplate
				.MatchProtocol(agents.UtilsAgents.PROTOCOL_QUERY);
		MessageTemplate mt2 = MessageTemplate
				.MatchPerformative(ACLMessage.QUERY_REF);
		agent.addBehaviour(new RecieveFinishL(agent, MessageTemplate.and(mt1,
				mt2)));
	}

	public class RecieveFinishL extends AchieveREResponder {
		@Override
		protected ACLMessage prepareResultNotification(ACLMessage arg0,
				ACLMessage arg1) throws FailureException {
			return null;
		}

		/**
         *
         */
		private static final long serialVersionUID = 1L;

		public RecieveFinishL(Agent arg0, MessageTemplate arg1) {
			super(arg0, arg1);
		}

		@Override
		protected ACLMessage prepareResponse(ACLMessage arg0)
				throws NotUnderstoodException, RefuseException {
			Cell cellRecyclingCenter = new Cell(Cell.FREE);
			ACLMessage r = arg0.createReply();

			try {
				cellRecyclingCenter = (Cell) arg0.getContentObject();
				if (cellRecyclingCenter == null)
					System.out
							.println("Harvester Manager: Not found any recycling center for ");
				else
					System.out.println("door at " + cellRecyclingCenter);

				r.setPerformative(ACLMessage.AGREE);
			} catch (UnreadableException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			try {
				r.setContentObject(cellRecyclingCenter);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return r;
		}
	}

	public InfoGame getGame() {
		return game;
	}

	public void setGame(InfoGame game) {
		this.game = game;
	}
}
